using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class BicycleManufacturersController : Controller
    {
        public BicycleManufacturersController(IRepository<BicycleManufacturer> bicycleManufacturerRepository) {
            Check.Require(bicycleManufacturerRepository != null, "bicycleManufacturerRepository may not be null");

            this.bicycleManufacturerRepository = bicycleManufacturerRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<BicycleManufacturer> bicycleManufacturers = bicycleManufacturerRepository.GetAll();
            return View(bicycleManufacturers);
        }

        [Transaction]
        public ActionResult Show(int id) {
            BicycleManufacturer bicycleManufacturer = bicycleManufacturerRepository.Get(id);
            return View(bicycleManufacturer);
        }

        public ActionResult Create() {
            BicycleManufacturerFormViewModel viewModel = BicycleManufacturerFormViewModel.CreateBicycleManufacturerFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(BicycleManufacturer bicycleManufacturer) {
            if (ViewData.ModelState.IsValid && bicycleManufacturer.IsValid()) {
                bicycleManufacturerRepository.SaveOrUpdate(bicycleManufacturer);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The bicycleManufacturer was successfully created.";
                return RedirectToAction("Index");
            }

            BicycleManufacturerFormViewModel viewModel = BicycleManufacturerFormViewModel.CreateBicycleManufacturerFormViewModel();
            viewModel.BicycleManufacturer = bicycleManufacturer;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            BicycleManufacturerFormViewModel viewModel = BicycleManufacturerFormViewModel.CreateBicycleManufacturerFormViewModel();
            viewModel.BicycleManufacturer = bicycleManufacturerRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(BicycleManufacturer bicycleManufacturer) {
            BicycleManufacturer bicycleManufacturerToUpdate = bicycleManufacturerRepository.Get(bicycleManufacturer.Id);
            TransferFormValuesTo(bicycleManufacturerToUpdate, bicycleManufacturer);

            if (ViewData.ModelState.IsValid && bicycleManufacturer.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The bicycleManufacturer was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                bicycleManufacturerRepository.DbContext.RollbackTransaction();

				BicycleManufacturerFormViewModel viewModel = BicycleManufacturerFormViewModel.CreateBicycleManufacturerFormViewModel();
				viewModel.BicycleManufacturer = bicycleManufacturer;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(BicycleManufacturer bicycleManufacturerToUpdate, BicycleManufacturer bicycleManufacturerFromForm) {
			bicycleManufacturerToUpdate.Name = bicycleManufacturerFromForm.Name;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The bicycleManufacturer was successfully deleted.";
            BicycleManufacturer bicycleManufacturerToDelete = bicycleManufacturerRepository.Get(id);

            if (bicycleManufacturerToDelete != null) {
                bicycleManufacturerRepository.Delete(bicycleManufacturerToDelete);

                try {
                    bicycleManufacturerRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the bicycleManufacturer from being deleted. " +
						"Another item likely depends on this bicycleManufacturer.";
                    bicycleManufacturerRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The bicycleManufacturer could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the BicycleManufacturer form for creates and edits
		/// </summary>
        public class BicycleManufacturerFormViewModel
        {
            private BicycleManufacturerFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static BicycleManufacturerFormViewModel CreateBicycleManufacturerFormViewModel() {
                BicycleManufacturerFormViewModel viewModel = new BicycleManufacturerFormViewModel();
                
                return viewModel;
            }

            public BicycleManufacturer BicycleManufacturer { get; internal set; }
        }

        private readonly IRepository<BicycleManufacturer> bicycleManufacturerRepository;
    }
}
